package com.application.Controller;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;

import android.util.Log;

public class ThreadReceiveFile extends Thread{
	String _path = "";
	String address = "";
	int port;
	int choice = 0;
	boolean _isRun = true;
	private String pathChatFile = null;
	private String fName = null;
	private String lineToRemove = null;
	public void SetReceive(String path, String address, int port)
	{
		choice = 1;
		this._path = path;
		this.address = address;
		this.port = port;
	}
	
	@Override
	public void run()
	{
		try{	
			while(true)
			{
				if(choice == 1 && _isRun == true)
				{
					Thread runThread = Thread.currentThread();
					Receive(_path, address, port);
					
					Thread.sleep(200);
					Pause();
					this.suspend();
				}
			}
		}
		catch(Exception ex)
		{
		}
	}
	
	public void Resume()
	{
		_isRun = true;
	}
	public void Pause()
	{
		_isRun = false;
	}
	public void Receive(String path,String Address,int port)
	 {
		 Log.d("PEERDROID","count thread receive file trc: " + Thread.activeCount());
		  Log.d("PEERDROID","sau InputStream:" + path);
		  choice = 0;
		try {
			int filesize = 6022386; // KÍch thướt file  tạm thời bị hashcode(băm ra)
		    long start = System.currentTimeMillis();
		    int bytesRead;
		    int current = 0;
		    // localhost for testing
		    Socket sock = new Socket(Address,port);
		    System.out.println("Connecting...");
	
		    // receive file
		    byte [] mybytearray  = new byte [filesize];
		    
		    InputStream is = sock.getInputStream();
		    FileOutputStream fos = new FileOutputStream(path);
		    BufferedOutputStream bos = new BufferedOutputStream(fos);
		    bytesRead = is.read(mybytearray,0,mybytearray.length);
		    current = bytesRead;
		    do {
		       bytesRead =is.read(mybytearray, current, (mybytearray.length-current));
		       if(bytesRead >= 0) current += bytesRead;
		    } while(bytesRead > -1);
	
		    bos.write(mybytearray, 0 , current);
		    bos.flush();
		    long end = System.currentTimeMillis();
		    System.out.println(end-start);
		    bos.close();
		    sock.close();
		    Log.d("PEERDROID","ket thuc nhan");
		    ReadWriteFileController.removeLineFromFile(pathChatFile, lineToRemove);
		    ReadWriteFileController.writeTXTPath(pathChatFile, "##notify##:Đã nhận được file: " + fName);
		    ActivityController.handler.post(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					NetworkController.init_runnableUpdateMsg();
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		this.stop();
		 Log.d("PEERDROID","count thread receive file sau: " + Thread.activeCount());
	 }

	public void setPathChatFile(String path) {
		this.pathChatFile = path;
	}

	public String getPathChatFile() {
		return pathChatFile;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getfName() {
		return fName;
	}

	public void setLineToRemove(String lineToRemove) {
		this.lineToRemove = lineToRemove;
	}

	public String getLineToRemove() {
		return lineToRemove;
	}
}
